package com.huizu.project.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import cn.hutool.core.collection.CollUtil;
import com.huizu.common.enums.LaborEnum;
import com.huizu.common.exception.ServiceException;
import com.huizu.common.utils.DateUtils;
import com.huizu.common.utils.SnowFlake;
import com.huizu.common.utils.StringUtils;
import com.huizu.project.domain.*;
import com.huizu.project.mapper.*;
import com.huizu.project.service.IBurialPointService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.huizu.project.service.IProLdService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 宿舍/教/实验劳动Service业务层处理
 * 
 * @author zhangmx
 * @date 2024-05-10
 */
@Service
public class ProLdServiceImpl implements IProLdService 
{
    @Autowired
    private ProLdMapper proLdMapper;

    @Autowired
    private ProLdUserMapper proLdUserMapper;

    @Autowired
    private ProUserLdPointsMapper proUserLdPointsMapper;

    @Autowired
    private ProLdConfigMapper proLdConfigMapper;

    @Autowired
    private ProLdWcMapper proLdWcMapper;

    @Autowired
    private ProLdJbMapper proLdJbMapper;

    @Autowired
    private ProLdDjMapper proLdDjMapper;

    @Autowired
    private ProLdFsMapper proLdFsMapper;

    @Autowired
    private ProUserMapper proUserMapper;

    @Autowired
    private ProXxtzMapper proXxtzMapper;

    @Autowired
    private ProFileMapper proFileMapper;

    @Autowired
    private IBurialPointService burialPointService;


    /**
     * 查询宿舍/教/实验劳动
     * 
     * @param id 宿舍/教/实验劳动主键
     * @return 宿舍/教/实验劳动
     */
    @Override
    public ProLd selectProLdById(String id) {
        ProLd proLd = proLdMapper.selectProLdById(id);
        proLd.setProjectName("基础劳动");
        ProLdUser proLdUser = new ProLdUser();
        proLdUser.setpId(proLd.getId());
        List<ProLdUser> proLdUsers = proLdUserMapper.selectProLdUserList(proLdUser);
        proLd.setProLdUserList(proLdUsers);
        ProFile proFile = new ProFile();
        proFile.setBusinessId(id);
        proLd.setProFileList(proFileMapper.selectProFileList(proFile));
        return proLd;
    }

    /**
     * 查询宿舍/教/实验劳动列表
     * 
     * @param proLd 宿舍/教/实验劳动
     * @return 宿舍/教/实验劳动
     */
    @Override
    public List<ProLd> selectProLdList(ProLd proLd)
    {
        return proLdMapper.selectProLdList(proLd);
    }

    /**
     * 新增宿舍/教/实验劳动
     * 
     * @param proLd 宿舍/教/实验劳动
     * @return 结果
     */
    @Override
    public int insertProLd(ProLd proLd)
    {
        proLd.setCreateTime(DateUtils.getNowDate());
        return proLdMapper.insertProLd(proLd);
    }

    /**
     * 修改宿舍/教/实验劳动
     * 
     * @param proLd 宿舍/教/实验劳动
     * @return 结果
     */
    @Override
    public int updateProLd(ProLd proLd)
    {

        return proLdMapper.updateProLd(proLd);
    }

    /**
     * 批量删除宿舍/教/实验劳动
     * 
     * @param ids 需要删除的宿舍/教/实验劳动主键
     * @return 结果
     */
    @Override
    public int deleteProLdByIds(String[] ids)
    {
       for (int i=0;i<ids.length;i++){
          String id = ids[i];
            ProLd proLd = proLdMapper.selectProLdById(id);
            if(proLd.getStatus().equals("2")){
                proUserLdPointsMapper.deleteProUserLdPoints(proLd.getId());
            }

        }
        return proLdMapper.deleteProLdByIds(ids);
    }

    /**
     * 删除宿舍/教/实验劳动信息
     * 
     * @param id 宿舍/教/实验劳动主键
     * @return 结果
     */
    @Override
    public int deleteProLdById(String id)
    {
        return proLdMapper.deleteProLdById(id);
    }

    public int pass(ProLd proLdPram){
        proLdPram.setStatus("2");
        ProLd proLd = proLdMapper.selectProLdById(proLdPram.getId());
        //检查上限
        ProLdConfig proLdConfig = proLdConfigMapper.selectProLdConfigById(proLd.getType());
        ProUser proUser = proUserMapper.selectProUserByUserId(proLd.getUserId());
        if (proUser==null)
            throw new ServiceException("用户已不存在");
        ProUserLdPoints proUserLdPointsPram = new ProUserLdPoints();
        proUserLdPointsPram.setUserId(proLd.getUserId());
        proUserLdPointsPram.setType(proLd.getType());
        //用户当前项目得分
        int userSumType = proUserLdPointsMapper.selectProUserLdPointsList(proUserLdPointsPram).stream().map(ProUserLdPoints::getPoints).mapToInt(Integer::intValue).sum();
        int maxPoints = proUser.getMaxPoints();
        //超过总分
        if (proLd.getDfCount()+proUser.getUserPoints()>maxPoints){
            proLd.setDfCount(maxPoints-proUser.getUserPoints());
        }
        //超过对应分
        if (proLd.getDfCount()+userSumType>proLdConfig.getMaxPoints()){
            proLd.setDfCount(proLdConfig.getMaxPoints()-userSumType);
        }
        proLdPram.setOpTime(new Date());
        proLdPram.setDfCount(proLd.getDfCount());
        proLdMapper.updateProLd(proLdPram);

        ProUserLdPoints proUserLdPoints  =new ProUserLdPoints();
        proUserLdPoints.setId(String.valueOf(SnowFlake.nextId()));
        proUserLdPoints.setUserId(proLd.getUserId());
        proUserLdPoints.setType(proLd.getType());
        proUserLdPoints.setPoints(proLd.getDfCount());
        proUserLdPoints.setCreateTime(new Date());
        proUserLdPoints.setProLdId(proLd.getId());
       return proUserLdPointsMapper.insertProUserLdPoints(proUserLdPoints);
    }

    public int reject(ProLd proLd){
        proLd.setStatus("3");
        proLd.setOpTime(new Date());
        return proLdMapper.updateProLd(proLd);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insertProLdApi(ProLd proLd) {
        String type = proLd.getType();
        int dfCount = 0;
        String markNumber = null;
        ProLdConfig proLdConfig = proLdConfigMapper.selectProLdConfigById(proLd.getType());
        if (proLdConfig==null)
            throw new ServiceException("请选择正确的类型");
        dfCount = Math.toIntExact(proLdConfig.getPoints());

        if ("5".equals(type)){
            ProLdJb proLdJb = proLdJbMapper.selectProLdJbByName(proLd.getJbName());
            if (proLdJb!=null){
                dfCount = proLdJb.getMarkNumber().intValue();
                markNumber = proLdJb.getMarkNumber().toString();
            }
        }
        //互联网+
        if ("6".equals(type)){
            ProLdJb proLdJb = proLdJbMapper.selectProLdJbByName(proLd.getJbName());
            if (proLdJb!=null){
                dfCount = proLdJb.getMarkNumber().intValue();
                markNumber = proLdJb.getMarkNumber().toString();
            }
            ProLdDj proLdDj = proLdDjMapper.selectProLdDjByName(proLd.getDjName());
            if (proLdDj!=null){
                dfCount = proLdDj.getMarkNumber().multiply(BigDecimal.valueOf(dfCount)).intValue();
                markNumber = proLdDj.getMarkNumber().toString();
            }
            ProLdWc proLdWc = proLdWcMapper.selectProLdWcByName(proLd.getWcName());
            if (proLdWc!=null){
                dfCount = proLdWc.getMarkNumber().multiply(BigDecimal.valueOf(dfCount)).intValue();
                markNumber = proLdWc.getMarkNumber().toString();
            }
        }else if ("7".equals(type)){
            ProLdJb proLdJb = proLdJbMapper.selectProLdJbByName(proLd.getJbName());
            if (proLdJb!=null){
                dfCount = proLdJb.getMarkNumber().intValue();
                markNumber = proLdJb.getMarkNumber().toString();
            }
            ProLdDj proLdDj = proLdDjMapper.selectProLdDjByName(proLd.getDjName());
            if (proLdDj!=null){
                dfCount = proLdDj.getMarkNumber().multiply(BigDecimal.valueOf(dfCount)).intValue();
                markNumber = proLdDj.getMarkNumber().toString();
            }
        }
        proLd.setMarkNumber(markNumber);
        proLd.setDfCount(dfCount);
        proLd.setId(String.valueOf(SnowFlake.nextId()));
        proLd.setCreateTime(new Date());
        proLd.setStatus("1");
        insertProLdUser(proLd);
        insertProFiles(proLd.getUserId(),proLd.getId(),"jcld",proLd.getProFileList());
        int row = proLdMapper.insertProLd(proLd);

        // 添加埋点
        ProUser proUser = proUserMapper.selectProUserByUserId(proLd.getUserId());
        if (proUser == null) {
            throw new ServiceException("未查询到此学生信息");
        }
        BurialPoint burialPoint = BurialPoint.builder()
                .userId(proLd.getUserId())
                .type(LaborEnum.BASIC_LABOR.getValue())
                .businessId(proLd.getId())
                .xyId(proUser.getXyId())
                .build();
        burialPointService.insertBurialPoint(burialPoint);

        return row;
    }

    public void insertProFiles(String userId,String businessId,String type,List<ProFile> proFiles){
        if (CollUtil.isNotEmpty(proFiles)){
            proFiles.forEach(proFile->{
                proFile.setId(String.valueOf(SnowFlake.nextId()));
                proFile.setUserId(userId);
                proFile.setBusinessId(businessId);
                proFile.setType(type);
                proFileMapper.insertProFile(proFile);
            });
        }
    }

    @Override
    public int repulse(ProLd proLd) {
        // 消除通过后的得分
        proUserLdPointsMapper.deleteProUserLdPoints(proLd.getId());
        proLd.setOpTime(new Date());
        // 设置为待审核状态
        proLd.setStatus("3");
        return proLdMapper.updateProLd(proLd);
    }

    public int updateProLdApi(ProLd proLd) {
        proFileMapper.deleteProFileByBusinessId(proLd.getId());
        insertProFiles(proLd.getUserId(),proLd.getId(),"jcld",proLd.getProFileList());
        proLd.setCreateTime(new Date());
        proLd.setStatus("1");
        return proLdMapper.updateProLd(proLd);
    }

    /**
     * 查询宿舍/教/实验劳动列表
     *
     * @param proLd 宿舍/教/实验劳动
     * @return 宿舍/教/实验劳动集合
     */
    public List<ProLd> selectProLdListApi(ProLd proLd){
        return proLdMapper.selectProLdList(proLd);
    }

    /**
     * 查询宿舍/教/实验劳动列表
     *
     * @param proLd 宿舍/教/实验劳动
     * @return 宿舍/教/实验劳动集合
     */
    public List<ProLd> selectProLdListReApi(ProLd proLd){
        List<ProLd> proLds = proLdMapper.selectProLdList(proLd);
        for (ProLd ld : proLds) {
            ProXxtz proXxtz = new ProXxtz();
            proXxtz.setId(String.valueOf(SnowFlake.nextId()));
            proXxtz.setUserId(proLd.getBeUserId());
            proXxtz.setpId(ld.getId());
            proXxtz.setCreateTime(new Date());
            proXxtzMapper.insertProXxtz(proXxtz);
        }
        return proLds;
    }

    /**
     * 查询宿舍/教/实验劳动列表
     *
     * @param proLd 宿舍/教/实验劳动
     * @return 宿舍/教/实验劳动集合
     */
    public List<ProLd> selectProLdListTerApi(ProLd proLd){
        return proLdMapper.selectProLdList(proLd);
    }

    public List<ProLd> selectProLdAllListTerApi(ProLd proLd){

        return proLdMapper.selectProLdAllListTerApi(proLd);
    }




    /**
     * 新增劳动关联用户信息
     *
     * @param proLd 宿舍/教/实验劳动对象
     */
    public void insertProLdUser(ProLd proLd)
    {
        List<ProLdUser> proLdUserList = proLd.getProLdUserList();
        String id = proLd.getId();
        if (StringUtils.isNotNull(proLdUserList))
        {
            List<ProLdUser> list = new ArrayList<ProLdUser>();
            for (ProLdUser proLdUser : proLdUserList)
            {
                proLdUser.setId(String.valueOf(SnowFlake.nextId()));
                proLdUser.setpId(id);
                list.add(proLdUser);
            }
            if (list.size() > 0)
            {
                proLdUserMapper.batchProLdUser(list);
            }
        }
    }

//    @Override
//    public int insertProLd2Api(ProLd2 proLd) {
//        proLd.setId(String.valueOf(SnowFlake.nextId()));
//        proLd.setCreateTime(new Date());
//        proLd.setStatus("1");
//        return proLd2Mapper.insertProLd2(proLd);
//    }
//
//    @Override
//    public int insertProLd3Api(ProLd3 proLd) {
//        proLd.setId(String.valueOf(SnowFlake.nextId()));
//        proLd.setCreateTime(new Date());
//        proLd.setStatus("1");
//        return proLd3Mapper.insertProLd3(proLd);
//    }
//
//    @Override
//    public int insertProLd4Api(ProLd4 proLd) {
//        proLd.setId(String.valueOf(SnowFlake.nextId()));
//        proLd.setCreateTime(new Date());
//        proLd.setStatus("1");
//        return proLd4Mapper.insertProLd4(proLd);
//    }
//
//    @Override
//    public int insertProLd5Api(ProLd5 proLd) {
//        proLd.setId(String.valueOf(SnowFlake.nextId()));
//        proLd.setCreateTime(new Date());
//        proLd.setStatus("1");
//        return proLd5Mapper.insertProLd5(proLd);
//    }
//
//    @Override
//    public int insertProLd6Api(ProLd6 proLd) {
//        proLd.setId(String.valueOf(SnowFlake.nextId()));
//        proLd.setCreateTime(new Date());
//        proLd.setStatus("1");
//        return proLd6Mapper.insertProLd6(proLd);
//    }
//
//    @Override
//    public int insertProLd7Api(ProLd7 proLd) {
//        proLd.setId(String.valueOf(SnowFlake.nextId()));
//        proLd.setCreateTime(new Date());
//        proLd.setStatus("1");
//        return proLd7Mapper.insertProLd7(proLd);
//    }
//
//    @Override
//    public int insertProLd8Api(ProLd8 proLd) {
//        proLd.setId(String.valueOf(SnowFlake.nextId()));
//        proLd.setCreateTime(new Date());
//        proLd.setStatus("1");
//        return proLd8Mapper.insertProLd8(proLd);
//    }
//
//    @Override
//    public int insertProLd9Api(ProLd9 proLd) {
//        proLd.setId(String.valueOf(SnowFlake.nextId()));
//        proLd.setCreateTime(new Date());
//        proLd.setStatus("1");
//        return proLd9Mapper.insertProLd9(proLd);
//    }
//
//    @Override
//    public int insertProLd10Api(ProLd10 proLd) {
//        proLd.setId(String.valueOf(SnowFlake.nextId()));
//        proLd.setCreateTime(new Date());
//        proLd.setStatus("1");
//        return proLd10Mapper.insertProLd10(proLd);
//    }
//
//    @Override
//    public int insertProLd11Api(ProLd11 proLd) {
//        proLd.setId(String.valueOf(SnowFlake.nextId()));
//        proLd.setCreateTime(new Date());
//        proLd.setStatus("1");
//        return proLd11Mapper.insertProLd11(proLd);
//    }
}
